package com.inossem.warehousemanage_tv.fragment;

import android.widget.GridView;
import android.widget.TextView;

import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.adapter.MainAdapter;
import com.inossem.warehousemanage_tv.bean.BaseBean;
import com.inossem.warehousemanage_tv.bean.MainBean;
import com.inossem.warehousemanage_tv.custom.RiseNumberTextView;
import com.inossem.warehousemanage_tv.utils.Api;
import com.inossem.warehousemanage_tv.utils.ToastUtils;
import com.inossem.warehousemanage_tv.utils.requestUtils.InossemCallback;
import com.inossem.warehousemanage_tv.utils.requestUtils.RetrofitUtils;

import java.util.List;

import retrofit2.Response;


/**
 * 仓库概览-首页
 *
 * @author 詹建宇
 * @time on 2019/2/13 16:43
 * @email jianyu.zhan@inossem.com
 */
public class WareMainFragment extends BaseFragment {

    private RiseNumberTextView inNumTv, outNumTv, putNumTv;
    private TextView temporaryTv;
    private GridView gridView;
    private List<MainBean.LocationListBean> list;

    @Override
    public int initLayout() {
        return R.layout.fragment_ware_main;
    }

    @Override
    public void initView() {
        inNumTv = mView.findViewById(R.id.inNumTv);
        outNumTv = mView.findViewById(R.id.outNumTv);
        putNumTv = mView.findViewById(R.id.putNumTv);
        temporaryTv = mView.findViewById(R.id.temporaryTv);
        gridView = mView.findViewById(R.id.gridView);

        temporaryTv.setText("临\n时\n存\n储\n区");
    }

    @Override
    public void initData() {
        RetrofitUtils.getRetrofit(mActivity, "詹建宇", "刘宇", "仓库概览", "获取待办事项及库存地点")
                .create(Api.class)
                .getLocationList("2")
                .enqueue(new InossemCallback<BaseBean<MainBean>>() {
                    @Override
                    public void success(Response<BaseBean<MainBean>> response) {
                        MainBean bean = response.body().getBody();
                        mActivity.factory_id = bean.getFactory_id();
                        inNumTv.setText(bean.getIn_number());
                        outNumTv.setText(bean.getOut_number());
//                        putNumTv.setText(bean.getUp_number());
                        RiseNumberTextView[] tvs = new RiseNumberTextView[]{inNumTv, outNumTv};
                        for (RiseNumberTextView tv : tvs) {
                            int num = Integer.parseInt(tv.getText().toString().trim());
                            tv.start(num, num * 30);
                        }
                        list = bean.getLocation_list();
                        if (null != list && !list.isEmpty())
                            gridView.setAdapter(new MainAdapter(mActivity, list));
                    }

                    @Override
                    public void failure(Response<BaseBean<MainBean>> response) {
                        ToastUtils.showToast(response.body().getHead().getMsg());
                    }
                });
    }

    @Override
    public void initClick() {
        temporaryTv.setOnClickListener(v -> {
            mActivity.isTemporary = true;
            mActivity.startFragment(new WareMatsFragment());
        });

        gridView.setOnItemClickListener((parent, view, position, id) -> {
            mActivity.location_id = list.get(position).getLocation_id();
            mActivity.isTemporary = false;
            mActivity.locationName = list.get(position).getLocation_name();
            mActivity.startFragment(new WareShelvesFragment());
        });
    }

}
